#include "stdafx.h"
#include "JPiece.h"
#include "JBoundary.h"
#include "JPoint.h"

JPiece::JPiece()
{
}


JPiece::~JPiece()
{
}

void JPiece::AddBoundary(JBoundary* _pBoundary)
{
	m_pBoundaries.push_back(_pBoundary);
}

void JPiece::GetBoundrayBox(CRect& _rc)
{
	if (m_pBoundaries.empty())
	{
		return;
	}

	_rc.left = _rc.right = m_pBoundaries[0]->GetPoint(0)->GetX();
	_rc.top = _rc.bottom = m_pBoundaries[0]->GetPoint(0)->GetY();

	int num = (int)m_pBoundaries.size();
	for (int i = 0; i < num; ++i)
	{
		m_pBoundaries[i]->GenerateBoundaryBox(_rc);
	}
}

void JPiece::GenerateBoundaryPath(vector<CPoint>& _pos)
{
	if (m_pBoundaries.empty())
	{
		return;
	}
	_pos.clear();

	//ResortBoundaries();
	
	int num = (int)m_pBoundaries.size();
	for (int i = 0; i < num; ++i)
	{
		m_pBoundaries[i]->GeneratePath(_pos, m_pPoints[i]);
	}
}

CPoint JPiece::GetCenterPos()
{
	CRect rc;
	GetBoundrayBox(rc);
	CPoint pt((rc.left + rc.right) / 2,
		(rc.top + rc.bottom) / 2);
	return pt;
}

void JPiece::ResortBoundaries()
{
	vector<JBoundary*> pSequenceBoundary;
	vector<JBoundary*> pBoundaries;
	m_pPoints.clear();

	pBoundaries.assign(m_pBoundaries.begin(), m_pBoundaries.end());

	pSequenceBoundary.push_back(pBoundaries.front());
	m_pPoints.push_back(pBoundaries[0]->GetPoint(0));
	pBoundaries.erase(pBoundaries.begin());

	vector<JBoundary*>::iterator it, itend;
	while (!pBoundaries.empty())
	{
		it = pBoundaries.begin();
		itend = pBoundaries.end();
		while (it != itend)
		{
			if ((*it)->GetPoint(0) == pSequenceBoundary.back()->GetPoint(1))
			{
				m_pPoints.push_back((*it)->GetPoint(0));
				pSequenceBoundary.push_back(*it);
				pBoundaries.erase(it);
				break;
			}
			else if ((*it)->GetPoint(1) == pSequenceBoundary.back()->GetPoint(1))
			{
				m_pPoints.push_back((*it)->GetPoint(1));
				pSequenceBoundary.push_back(*it);
				pBoundaries.erase(it);
				break;
			}
			else if ((*it)->GetPoint(0) == pSequenceBoundary.front()->GetPoint(0))
			{
				m_pPoints.insert(m_pPoints.begin(), (*it)->GetPoint(1));
				pSequenceBoundary.insert(pSequenceBoundary.begin(), *it);
				pBoundaries.erase(it);
				break;
			}
			else if ((*it)->GetPoint(1) == pSequenceBoundary.front()->GetPoint(0))
			{
				m_pPoints.insert(m_pPoints.begin(), (*it)->GetPoint(0));
				pSequenceBoundary.insert(pSequenceBoundary.begin(), *it);
				pBoundaries.erase(it);
				break;
			}
			++it;
		}
	}
	m_pBoundaries.assign(pSequenceBoundary.begin(), pSequenceBoundary.end());
}

int JPiece::GetPointCount()
{
	return (int)m_pPoints.size();
}

JPoint* JPiece::GetPoint(int _idx)
{
	return m_pPoints[_idx];
}

int JPiece::GetCtrlPointPos(vector<CPoint>& _pos)
{
	_pos.clear();
	int num = (int)m_pPoints.size();
	for (int i = 0; i < num; ++i)
	{
		_pos.push_back(CPoint(m_pPoints[i]->GetX(), m_pPoints[i]->GetY()));
	}
	return num;
}

int JPiece::GetPointIdx(JPoint* _pPoint)
{
	int num = (int)m_pPoints.size();
	for (int i = 0; i < num; ++i)
	{
		if (m_pPoints[i] == _pPoint)
		{
			return i;
		}
	}
	return -1;
}

int JPiece::GetBoundaryCount()
{
	return (int)m_pBoundaries.size();
}

JBoundary* JPiece::GetBoundary(int _idx)
{
	return m_pBoundaries[_idx];
}

bool JPiece::IsShareBoundary(JBoundary* _pBoundary)
{
	return find(m_pBoundaries.begin(), m_pBoundaries.end(), _pBoundary) != m_pBoundaries.end();
}
